package org.linlinjava.litemall.db.service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallCart;
import org.linlinjava.litemall.db.mapper.LitemallCartDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallCartService {
    @Resource
    private LitemallCartDao cartDao;

    public LitemallCart queryExist(Integer goodsId, Integer productId, Integer userId) {
    	
    	LitemallCart cart = new LitemallCart();
    	cart.setGoodsId(goodsId);
    	cart.setProductId(productId);
    	cart.setUserId(userId);
    	return cartDao.templateOne(cart);
		/*
		 * LitemallCartExample example = new LitemallCartExample();
		 * example.or().andGoodsIdEqualTo(goodsId).andProductIdEqualTo(productId).
		 * andUserIdEqualTo(userId).andDeletedEqualTo(false); return
		 * cartMapper.selectOneByExample(example);
		 */
    }

    public void add(LitemallCart cart) {
        cart.setAddTime(LocalDateTime.now());
        cart.setUpdateTime(LocalDateTime.now());
        cartDao.insertTemplate(cart,true);
        //cartMapper.insertSelective(cart);
    }

    public int updateById(LitemallCart cart) {
        cart.setUpdateTime(LocalDateTime.now());
        return cartDao.updateById(cart);
        //return cartMapper.updateByPrimaryKeySelective(cart);
    }

    public List<LitemallCart> queryByUid(int userId) {
    	LitemallCart cart = new LitemallCart();
    	cart.setUserId(userId);
    	cart.setDeleted(false);
    	return cartDao.template(cart);
		/*
		 * LitemallCartExample example = new LitemallCartExample();
		 * example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false); return
		 * cartMapper.selectByExample(example);
		 */
    }


    public List<LitemallCart> queryByUidAndChecked(Integer userId) {
    	
    	LitemallCart cart = new LitemallCart();
    	cart.setUserId(userId);
    	return cartDao.template(cart);
    	
		/*
		 * LitemallCartExample example = new LitemallCartExample();
		 * example.or().andUserIdEqualTo(userId).andCheckedEqualTo(true).
		 * andDeletedEqualTo(false); return cartMapper.selectByExample(example);
		 */
    }

    public int delete(List<Integer> productIdList, int userId) {
    	
    	
    	LitemallCart update = new LitemallCart();
    	update.setUpdateTime(LocalDateTime.now());
    	update.setDeleted(true);
    	return cartDao.createLambdaQuery().andIn(LitemallCart::getProductId, productIdList)
    	.andEq(LitemallCart::getUserId, userId).updateSelective(update);
    	
		/*
		 * LitemallCartExample example = new LitemallCartExample();
		 * example.or().andUserIdEqualTo(userId).andProductIdIn(productIdList); return
		 * cartMapper.logicalDeleteByExample(example);
		 */
    }

    public LitemallCart findById(Integer id) {
    	return cartDao.single(id);
        //return cartMapper.selectByPrimaryKey(id);
    }

    public LitemallCart findById(Integer userId, Integer id) {
    	
    	LitemallCart cart = new LitemallCart();
    	cart.setUserId(userId);
    	cart.setId(id);
    	cart.setDeleted(false);
    	return cartDao.templateOne(cart);
		/*
		 * LitemallCartExample example = new LitemallCartExample();
		 * example.or().andUserIdEqualTo(userId).andIdEqualTo(id).andDeletedEqualTo(
		 * false); return cartMapper.selectOneByExample(example);
		 */
    }

    public int updateCheck(Integer userId, List<Integer> idsList, Boolean checked) {
    	
    	LitemallCart update = new LitemallCart();
    	update.setUpdateTime(LocalDateTime.now());
    	update.setChecked(checked);
    	return cartDao.createLambdaQuery().andIn(LitemallCart::getProductId, idsList)
    	.andEq(LitemallCart::getUserId, userId).updateSelective(update);
    	
		/*
		 * LitemallCartExample example = new LitemallCartExample();
		 * example.or().andUserIdEqualTo(userId).andProductIdIn(idsList).
		 * andDeletedEqualTo(false); LitemallCart cart = new LitemallCart();
		 * cart.setChecked(checked); cart.setUpdateTime(LocalDateTime.now()); return
		 * cartMapper.updateByExampleSelective(cart, example);
		 */
    }

    public void clearGoods(Integer userId) {
    	LitemallCart cart = new LitemallCart();
    	cart.setUserId(userId);
    	cart.setDeleted(true);
    	cart.setUpdateTime(LocalDateTime.now());
    	cartDao.updateTemplateById(cart);
		/*
		 * LitemallCartExample example = new LitemallCartExample();
		 * example.or().andUserIdEqualTo(userId).andCheckedEqualTo(true); LitemallCart
		 * cart = new LitemallCart(); cart.setDeleted(true);
		 * cartMapper.updateByExampleSelective(cart, example);
		 */
    }

    public List<LitemallCart> querySelective(Integer userId, Integer goodsId, Integer page, Integer limit, String sort, String order) {
    	
    	LambdaQuery<LitemallCart> query = cartDao.createLambdaQuery();
    	 if (!StringUtils.isEmpty(userId)) {
    		 query.andEq(LitemallCart::getUserId, userId);
             //criteria.andUserIdEqualTo(userId);
         }
         if (!StringUtils.isEmpty(goodsId)) {
        	 query.andEq(LitemallCart::getGoodsId, goodsId);
             //criteria.andGoodsIdEqualTo(goodsId);
         }
         
         query.andEq(LitemallCart::getDeleted, false);
         //criteria.andDeletedEqualTo(false);

         if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	 query.orderBy(sort + " " + order);
         }
         query.limit(page, limit);
         List<LitemallCart> carts= query.select();
         return carts;
    	
			/*
			 * LitemallCartExample example = new LitemallCartExample();
			 * LitemallCartExample.Criteria criteria = example.createCriteria();
			 * 
			 * if (!StringUtils.isEmpty(userId)) { criteria.andUserIdEqualTo(userId); } if
			 * (!StringUtils.isEmpty(goodsId)) { criteria.andGoodsIdEqualTo(goodsId); }
			 * criteria.andDeletedEqualTo(false);
			 * 
			 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			 * example.setOrderByClause(sort + " " + order); }
			 * 
			 * PageHelper.startPage(page, limit); return
			 * cartMapper.selectByExample(example);
			 */
    }

    public void deleteById(Integer id) {
    	LitemallCart cart = new LitemallCart();
    	cart.setId(id);
    	cart.setDeleted(true);
    	cart.setUpdateTime(LocalDateTime.now());
    	cartDao.updateTemplateById(cart);
        //cartMapper.logicalDeleteByPrimaryKey(id);
    }

    public boolean checkExist(Integer goodsId) {
    	
    	LitemallCart cart = new LitemallCart();
    	cart.setGoodsId(goodsId);
    	cart.setDeleted(false);
    	cart.setChecked(true);
    	return cartDao.templateCount(cart)!=0;
		/*
		 * LitemallCartExample example = new LitemallCartExample();
		 * example.or().andGoodsIdEqualTo(goodsId).andCheckedEqualTo(true).
		 * andDeletedEqualTo(false); return cartMapper.countByExample(example) != 0;
		 */
    }

    public void updateProduct(Integer id, String goodsSn, String goodsName, BigDecimal price, String url) {
    	
    	LitemallCart update = new LitemallCart();
    	update.setUpdateTime(LocalDateTime.now());
    	update.setGoodsSn(goodsSn);
    	update.setPrice(price);
    	update.setPicUrl(url);
    	cartDao.createLambdaQuery().andEq(LitemallCart::getProductId, id).updateSelective(update);
    	
		/*
		 * 
		 * LitemallCart cart = new LitemallCart(); cart.setPrice(price);
		 * cart.setPicUrl(url); cart.setGoodsSn(goodsSn); cart.setGoodsName(goodsName);
		 * LitemallCartExample example = new LitemallCartExample();
		 * example.or().andProductIdEqualTo(id);
		 * cartMapper.updateByExampleSelective(cart, example);
		 */
    }
}
